$(function () {
	todoTasks();
});

function startProcess() {
	var url = "../selfflow/startproc.do";
	$.ajax({
		url : url,
		data : {},
		dataType : "json",
		type : "post",
		success : function(data) {
			if (data.procId) {
				$.messager.alert("系统提示", "发起流程成功！");
				todoTasks();
			} else {
				$.messager.alert("系统提示", "发起流程失败！");
			}
		}
	});
}

function queryAllProcs() {
	$('#dg-flowlist').datagrid({
		title : '流转中的流程',
		url : '../selfflow/queryallprocs.do',
		pagination : true,
		rownumbers : true,
		pagePosition:'bottom',
		pageList : [ 5, 10, 15, 20 ],
		pageSize: 10,
		fitColumns : true,
		toolbar:'#toolbar',
		columns : [ [ {
			field : 'id',
			title : '流程编号',
			width : 100
		}, {
			field : 'name',
			title : '流程类别',
			width : 100
		}, {
			field : 'operate',
			title : '操作',
			width : 30,
			align:'center',
			formatter:function(value, row){  
                var btn = "<a class='easyui-linkbutton flow-grid-btn' onclick='completeProc("+row.id+")' href='#' >结束</a>";  
                return btn;  
            }
		}
		] ]
	});
}
	
function completeProc(procId) {
	$.ajax({
		url : '../selfflow/completeprocinst.do',
		data : {
			procId: procId
		},
		dataType : "text",
		type : "post",
		success : function(data) {
			if(data == "success") {
				$.messager.alert("系统提示", "已经完成本流程");
				queryAllProcs();
			}
			else {
				$.messager.alert("系统提示", "未完成本流程");
			}
		}
	});
}

function todoTasks() {
	$('#dg-flowlist').datagrid({
		title : '待处理的任务',
		url : '../selfflow/gettasks.do',
		pagination : true,
		rownumbers : true,
		pagePosition:'bottom',
		pageList : [ 5, 10, 15, 20 ],
		pageSize: 10,
		fitColumns : true,
		toolbar:'#toolbar',
		columns : [ [ {
			field : 'id',
			title : '任务ID',
			width : 100
		}, 
		{
			field : 'procId',
			title : '流程ID',
			width : 100
		},
		{
			field : 'name',
			title : '当前步骤',
			width : 100
		}, {
			field : 'operate',
			title : '操作',
			width : 30,
			align:'center',
			formatter:function(value, row){  
                var btn = "<a class='easyui-linkbutton flow-grid-btn' onclick='getTaskForm(" + row.id+")' href='#' >处理</a>";  
                return btn;  
            }
		}
		] ]
	});
}

function getTaskForm(taskId) {
	var formPropertysMap = {
		"drafter": "顾客",
		"startDate": "开始日期",
		"startTime": "开始时间",
		"commodity": "商品",
		"amount": "交易金额",
		"postAddr": "邮寄地址"
   };
	
	var html = "";
	var url = "../selfflow/gettaskform.do";
	$.ajax({
		url : url,
		data : {
			taskId: taskId
		},
		dataType : "json",
		type : "post",
		success : function(result) {
			var map = result.formData;
			var progressNodes = result.progressAxis;
			html += generateProgressAxis(progressNodes);
			html += '<h4>当前步骤</h4>' + result.currentNode + '<br/>';
			html += '<h4>表单内容</h4><br/>';
			html += '<form id="Form1" method="post" action="../selfflow/completetaskwithform.do" >';
			for(var key in map) {
				if(map[key] == undefined) 
					map[key] = "";
				html += '<div><label>' 
					+ formPropertysMap[key] + '</label><div><input type="text" name="' + key +'" value="'
					+ map[key] + '"/></div></div>';
			}
			html += '<input name="taskId" type="hidden" value="' + taskId + '" />' +
			'<div><input type="submit" value="提交" /><input type="reset" value="重置" /></div></form>';
			

			$("#w").html(html);
			//
			//调用
		    $('#Form1').bind('submit', function(){
		        ajaxSubmit(this, function(data){
		            if(data == "success") {
		            	$("#w").window("close");
		            	todoTasks();
		            }
		        });
		        return false;
		    });
			$("#w").window('open');
		}
	});
}


function generateProgressAxis(array) {
	var html = '<h4>流转进度</h4><br/>';
	for(var i = 0; i < array.length; i++) {
		if(i == array.length - 1)
			html += '<label>' + array[i].name + '</label>';
		else
			html += '<label>' + array[i].name + '</label>' + '<label>&nbsp;---&gt;&nbsp;</label>';
	}
	return html;
}

//form的ajax形式提交
//ajax提交form函数
//将form转为AJAX提交
	function ajaxSubmit(frm, fn) {
	    var dataPara = getFormJson(frm);
	    $.ajax({
	        url: frm.action,
	        type: frm.method,
	        data: dataPara,
	        dataType: 'text',
	        success: fn
	    });
	}
	
//将form中的值转换为键值对。
function getFormJson(frm) {
    var o = {};
    var a = $(frm).serializeArray();
    $.each(a, function () {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });

    return o;
}